/**
 * Pinganfu.com Inc.
 * Copyright (c) 2013-2013 All Rights Reserved.
 */
package com.huatai.common.utils;

/**
 * 
 * @author kongdemeng
 * @version $Id: BusinessException.java, v 0.1 2013-10-18 上午11:13:13 kongdemeng Exp $
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author kongdemeng
 * @version $Id: CalendarUtil.java, v 0.1 2013-10-18 上午11:13:13 kongdemeng Exp $
 */
public final class CalendarUtil {

	protected static final Logger logger = LoggerFactory.getLogger(CalendarUtil.class);

	public static final String FORMAT1 = "yyyy-MM-dd";
	public static final String FORMAT2 = "yyyy/MM/dd";
	public static final String FORMAT3 = "yyyy年M月d日";
	public static final String FORMAT4 = "yyyyMMdd";
	public static final String FORMAT5 = "yyyy-MM-dd HH:mm:ss";
	public static final String FORMAT6 = "HH:mm:ss SSS";
	public static final String FORMAT7 = "MMddHHmmss";
	public static final String FORMAT8 = "ddHHmmssSSS";
	public static final String FORMAT9 = "yyyyMMddHHmmssSSS";
	public static final String FORMAT10 = "yyyy-MM-dd HH:mm:ss E";
	public static final String FORMAT11 = "yyyy-MM-dd HH:mm:ss SSS";
	public static final String FORMAT12 = "yyyyMMddHHmmss";
	public static final String HH = "HH";
	public static final String HHMMSS = "HH:mm:ss";
	public static final String YYYYMMDD = "yyyyMMdd";
	public static final String MMDDHHMMSS = "MMddHHmmss";
	public static final String FORMAT13 = "HHmmss";
	public static final String HHMM = "HH:mm";

	private CalendarUtil() {
	}

	/**
	 * 
	 * @param date
	 *            Date
	 * @param format
	 *            String
	 * @return String
	 */
	public static String formatDateByFormat(java.util.Date date, String format) {
		String result = "";
		if (date != null) {
			try {
				SimpleDateFormat sdf = new SimpleDateFormat(format);
				result = sdf.format(date);
			} catch (Exception ex) {
				logger.error("", ex);
			}
		}
		return result;
	}

	public static String addMonth(String dateStr, int month, String format) throws ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		Date date = null;
		date = dateFormat.parse(dateStr);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, month);
		Date nowDate = calendar.getTime();
		return dateFormat.format(nowDate);
	}

	/**
	 * 
	 * @param date
	 *            Date
	 * @return String
	 */

	public static String formatDate(java.util.Date date) {
		return formatDateByFormat(date, "yyyy-MM-dd");
	}

	/**
	 * 
	 * @param date
	 *            Date
	 * @return Long
	 * @throws ParseException
	 */

	public static Long getDate(String date, String format) throws ParseException {

		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date dd = sdf.parse(date);
		return new Long(dd.getTime());

	}

	public static String getStrDate(Long ld, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date dd = new Date(ld.longValue());
		return sdf.format(dd);
	}

	public static String getMonthBegin(String strdate) {
		java.util.Date date = parseFormatDate(strdate);
		return formatDateByFormat(date, "yyyy-MM") + "-01";
	}

	public static String getMonthEnd(String strdate) {
		java.util.Date date = parseFormatDate(getMonthBegin(strdate));
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, 1);
		calendar.add(Calendar.DAY_OF_YEAR, -1);
		return formatDate(calendar.getTime());
	}

	/**
	 * 把时间统一格式
	 * 
	 * @param strDate
	 * @return
	 */
	public static String getProcessDate(String strDate, String strFormat) {
		Date date = parseFormatTime(strDate);
		SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
		String returnDate = sdf.format(date);
		return returnDate;

	}

	public static Date parseFormatDate(String strDate) {

		java.util.Date date = null;

		if (strDate.indexOf('-') > 0) {
			date = parseFormatDateBy(strDate, "-");

		} else if (strDate.indexOf('.') > 0) {
			date = parseFormatDateBy(strDate, ".");

		} else if (strDate.indexOf(':') > 0) {
			date = parseFormatDateBy(strDate, ":");

		}
		return date;

	}

	public static Date parseFormatTime(String strDateIn) {

		java.util.Date date = null;
		// 去空格
		String strDate = strDateIn.replace(" ", "");

		if (strDate.indexOf('-') > 0) {
			date = parseFormatTimeBy(strDate, "-");

		} else if (strDate.indexOf('.') > 0) {
			date = parseFormatTimeBy(strDate, ".");

		} else if (strDate.indexOf(':') > 0) {
			date = parseFormatTimeBy(strDate, ":");

		} else if (strDate.length() > 14 && strDate.matches("[0-9]+")) {
			date = parseFormatDateOfstr(strDate);
		}
		return date;

	}

	/*
	 * 时间+n位随机数格式 截取入库
	 */
	public static Date parseFormatDateOfstr(String strDate) {
		java.util.Date date = null;
		String processDate = strDate.substring(0, 14);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			date = sdf.parse(processDate);
		} catch (Exception ex) {
			logger.error("", ex);
		}
		return date;
	}

	public static Date parseFormatTimeBy(String strDate, String division) {
		java.util.Date date = null;
		try {
			if ("-".equals(division) && strDate.indexOf(':') > 0 && strDate.length() == 18) {
				SimpleDateFormat sdf = new SimpleDateFormat(
						"yyyy" + division + "MM" + division + "dd" + "HH" + ":" + "mm" + ":" + "ss");
				date = sdf.parse(strDate);
			} else {
				SimpleDateFormat sdf = new SimpleDateFormat("HH" + division + "mm" + division + "ss");
				date = sdf.parse(strDate);
			}
		} catch (Exception ex) {
			logger.error("", ex);
		}
		return date;
	}

	public static Date parseFormatDateBy(String strDate, String division) {
		java.util.Date date = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy" + division + "MM" + division + "dd");
			date = sdf.parse(strDate);
		} catch (Exception ex) {
			logger.error("", ex);
		}
		return date;
	}

	public static Date parseFormatDateByFormat(String strDate, String strFormat) {
		java.util.Date date = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
			date = sdf.parse(strDate);
		} catch (Exception ex) {
			logger.error("", ex);
		}
		return date;
	}

	public static int getYear(java.util.Date date) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.get(java.util.Calendar.YEAR);
	}

	public static int getMonth(java.util.Date date) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.get(java.util.Calendar.MONTH) + 1;
	}

	public static int getDay(java.util.Date date) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.get(java.util.Calendar.DAY_OF_MONTH);
	}

	public static int getHour(java.util.Date date) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.get(java.util.Calendar.HOUR_OF_DAY);
	}

	public static int getMinute(java.util.Date date) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.get(java.util.Calendar.MINUTE);
	}

	public static int getSecond(java.util.Date date) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.get(java.util.Calendar.SECOND);
	}

	public static long getMillis(java.util.Date date) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		return c.getTimeInMillis();
	}

	public static java.util.Date addDate(java.util.Date date, int day) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
		return c.getTime();
	}

	public static int subDate(java.util.Date date, java.util.Date date1) {
		return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));
	}

	public static String addDate(String date, String format, int day) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		long d1 = sdf.parse(date).getTime() + (long) (day * 24 * 3600 * 1000);

		return formatDateByFormat(new Date(d1), format);
	}

	public static int compareDate(String date1, String date2, String format) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		long d1 = sdf.parse(date1).getTime();
		long d2 = sdf.parse(date2).getTime();
		if (d1 < d2) {
			return -1;
		} else if (d1 == d2) {
			return 0;
		} else {
			return 1;
		}
	}

	/**
	 * 日期相减
	 * 
	 * @param date
	 *            原日期
	 * @param days
	 *            需要减去的天数
	 * @param format
	 *            日期的格式
	 * @return 返回减去后的日期，格式与原日期的格式相同
	 * @throws ParseException
	 */
	public static String subDateByDays(String date, int days, String format) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		// 将原日期转换成毫秒
		long curDateTimes = sdf.parse(date).getTime();
		// 将需要减去的天数转换成毫秒
		long daysTimes = Long.parseLong(String.valueOf(days)) * 24 * 3600 * 1000;
		// 对毫秒进行相减，最后转换成需要的格式
		long result = curDateTimes - daysTimes;
		Date d = new Date();
		d.setTime(result);
		return sdf.format(d).toString();
	}
}